const pins = {};
import { Profile } from 'mixly';

Profile = Profile ?? {};
// pins.arduino_standard = {
//     description: "standard",
//     digital: [["0", "0"], ["1", "1"], ["2", "2"], ["3", "3"], ["4", "4"], ["5", "5"], ["6", "6"], ["7", "7"], ["8", "8"], ["9", "9"], ["10", "10"], ["11", "11"], ["12", "12"], ["13", "13"], ["A0", "A0"], ["A1", "A1"], ["A2", "A2"], ["A3", "A3"], ["A4", "A4"], ["A5", "A5"]],
//     analog: [["A0", "A0"], ["A1", "A1"], ["A2", "A2"], ["A3", "A3"], ["A4", "A4"], ["A5", "A5"], ["A6", "A6"], ["A7", "A7"]],
//     pwm: [["3", "3"], ["5", "5"], ["6", "6"], ["9", "9"], ["10", "10"], ["11", "11"]],
//     interrupt: [["2", "2"], ["3", "3"]],
//     SDA: [["A4", "A4"]],
//     SCL: [["A5", "A5"]],
//     MOSI: [["11", "11"]],
//     MISO: [["12", "12"]],
//     SCK: [["13", "13"]],
//     serial_select: [["Serial", "Serial"], ["SoftwareSerial", "mySerial"], ["SoftwareSerial1", "mySerial1"], ["SoftwareSerial2", "mySerial2"], ["SoftwareSerial3", "mySerial3"]],
//     serial: 9600
// };
if (typeof Profile.generate !== 'function') {
    Profile.parse = function (range) {
        let pinList = [];
        for (let i of range) {
            const pinInfo = i.split('-');
            let pinNumStr0, pinNumStr1;
            switch (pinInfo.length) {
            case 1:
                pinNumStr0 = pinInfo[0].toString();
                if (!isNaN(pinNumStr0)) {
                    const pinNum = parseInt(pinNumStr0);
                    pinList.push(pinNum);
                }
                break;
            case 2:
                pinNumStr0 = pinInfo[0].toString();
                pinNumStr1 = pinInfo[1].toString();
                if (!isNaN(pinNumStr0) && !isNaN(pinNumStr1)) {
                    let pinNum0 = parseInt(pinNumStr0);
                    let pinNum1 = parseInt(pinNumStr1);
                    if (pinNum0 < 0 || pinNum1 < 0) break;
                    if (pinNum0 > pinNum1) {
                        [ pinNum0, pinNum1 ] = [ pinNum1, pinNum0 ];
                    }
                    for (let j = pinNum0; j <= pinNum1; j++) {
                        if (!pinList.includes(j)) {
                            pinList.push(j);
                        }
                    }
                }
                break;
            }
        }
        return pinList;
    };

    Profile.generate = function (pinMap, add1 = '', add2 = '') {
        const getPins = (list) => {
            let pins = [];
            for (let i of list) {
                const pin = [ add1 + i, add2 + i ];
                pins.push(pin);
            }
            return pins;
        }
        const pinList = this.parse(pinMap);
        return getPins(pinList);
    };
}

pins.arduino_standard = {
    description: "standard",
    all: [
            ...Profile.generate(['0-15'], 'PA', 'PA'),
            ...Profile.generate(['0-15'], 'PB', 'PB'),
            ...Profile.generate(['0-15'], 'PC', 'PC'),
            ...Profile.generate(['0-2'], 'PD', 'PD')
        ],
    digital: [["左1按键", "PC12"], ["左2按键", "PC11"], ["左开关", "PC10"], ["左摇杆Z", "PD2"], ["左↑", "PB8"], ["左↓", "PB7"], ["左←", "PB5"], ["左→", "PB6"], ["左下按键", "PC14"],
                ["右1按键", "PA7"], ["右2按键", "PC4"], ["右开关", "PB0"], ["右摇杆Z", "PA5"], ["右↑", "PC3"], ["右↓", "PA0"], ["右←", "PA1"], ["右→", "PA2"], ["右下按键", "PC15"],
                ["OLED左按键", "PA12"], ["OLED右按键", "PA11"]],
    analog: [["左摇杆X", "PC2"], ["左摇杆Y", "PC1"], ["右摇杆X", "PA3"], ["右摇杆Y", "PA4"]],
    pwm: [["蜂鸣器", "PA6"]],
    interrupt: [["2", "2"], ["3", "3"]],
    SDA: [["A4", "A4"]],
    SCL: [["A5", "A5"]],
    MOSI: [["11", "11"]],
    MISO: [["12", "12"]],
    SCK: [["13", "13"]],
    serial_select: [["Serial", "Serial1"]],
    serial: 9600
};

export default pins;